package com.application.lucky.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.activity.me.RechargeCardActivity;
import com.application.lucky.activity.me.RechargeTypeActivity;
import com.application.lucky.bean.UserBean;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.widget.dialog.TwoButtonDialog;
import com.qunar.im.ui.custommessage.TransferBean;
import com.application.lucky.bean.jsonBean.FriendBean;
import com.application.lucky.bean.jsonBean.FriendListbean;
import com.application.lucky.manager.ContactsManager;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.request.UserRequest;
import com.application.lucky.request.nav.UrlManager;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.EventBusUtils;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.qunar.im.ui.util.ProfileUtils;

public class TransferActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private TextView mTvName;
    private EditText mEtMoneyNum;
    private TextView mTvShouxu;
    private TextView mTvMark;
    private Button mBtTransfer;
    private ImageView mIvHeadView;
    private String id;
    private FriendBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    @Override
    public void beforeInit() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        bean = ContactsManager.getInstance().searchFriendsByid(id);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_transfer;
    }

    private void initView() {
        mTitleBar = (TitlebarView) findViewById(R.id.title_bar);
        mTvName = (TextView) findViewById(R.id.tv_name);
        mEtMoneyNum = (EditText) findViewById(R.id.et_money_num);
        mTvShouxu = (TextView) findViewById(R.id.tv_shouxu);
        mTvMark = (TextView) findViewById(R.id.tv_mark);
        mBtTransfer = (Button) findViewById(R.id.bt_transfer);
        mIvHeadView = (ImageView) findViewById(R.id.iv_head_view);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        if (bean != null) {
            setui(bean.getShowName(), bean.getHeadPic());
        } else {
            new UserRequest().user_info(id, new CallBackEx<BaseResponse<UserBean>>() {
                @Override
                public void onFailed(int code) {

                }

                @Override
                public void onSuccess(BaseResponse<UserBean> userBeanBaseResponse) {
                    UserBean dataInfo = userBeanBaseResponse.dataInfo;
                    if (dataInfo != null) {
                        setui(dataInfo.getNickName(), dataInfo.getHeadPic());
                    }
                }
            });
        }

        mBtTransfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String s = mEtMoneyNum.getText().toString();
                if (TextUtils.isEmpty(s)) {
                    ToastUtils.show("请输入金额！");
                    return;
                }

                try {
                    money = Integer.valueOf(s);

                } catch (Exception e) {
                    money = 0;
                }
                if (money <= 0) {
                    ToastUtils.show("金额错误，请重新输入");
                    return;
                }
                mBtTransfer.setEnabled(false);

                new RedPRequest().red_transfer(money, id, new CallBackEx<BaseResponse<String>>() {
                    @Override
                    public void onSuccess(BaseResponse<String> stringBaseResponse) {
                        if (isFinishing()) {
                            return;
                        }
                        if (stringBaseResponse.dataInfo == null) {
                            return;
                        }
                        UserManagers.getInstance().loadBalance();
                        TransferBean transferBean = new TransferBean();
                        transferBean.id = id;
                        mBtTransfer.setEnabled(true);
                        transferBean.transferId = stringBaseResponse.dataInfo;
                        transferBean.money = money + "";
                        transferBean.time = System.currentTimeMillis();
                        EventBusUtils.post(transferBean);
                        finishAnim();
                        money = 0;
                    }

                    @Override
                    public void onFailed(int code) {
                        if (isFinishing()) {
                            return;
                        }
                        if (code == 60005) {
                            new TwoButtonDialog.Build(TransferActivity.this)
                                    .setMessage("余额不足，请充值！")
                                    .setLeftButton("取消", null)
                                    .setRightButton("确定", new TwoButtonDialog.BaseDialogClickListener() {
                                        @Override
                                        public void click() {
                                            startActivityAnim(new Intent(TransferActivity.this, RechargeCardActivity.class));
                                        }
                                    }).create().show();
                        }
                        mBtTransfer.setEnabled(true);
                        money = 0;
                    }
                });

            }
        });
    }

    Integer money;

    private void setui(String nickName, String headPic) {
        if (isFinishing()) {
            return;
        }
        mTvName.setText(nickName);
        ProfileUtils.displayGravatarByImageSrc(this, UrlManager.getHeadPic(headPic), mIvHeadView);
    }
}
